﻿using Ruandao;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web;
    
namespace TestRuandao
{
    using Ruandao.Setting;
    using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
    
    /// <summary>
    ///这是 SettingTest 的测试类，旨在
    ///包含所有 SettingTest 单元测试
    ///</summary>
    [TestClass()]
    public class SettingTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //static SettingManager<OptionDemo> optionManager;
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        [TestInitialize()]
        public void MyTestInitialize()
        {
            HttpContext.Current.User = new PrincipalForTest("Bob", "");
        }
        
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        [HostType("Asp.Net")]
        [UrlToTest("http://localhost/FoundationSite/UnitTest/Index.aspx")]
        public void SaveSystemTest()
        {
            var option = new OptionDemo("testOption");
            option.OneProperty = "System One Value...";
            option.SaveSystem();
            var actual = option.FetchSystem();
            Assert.AreEqual("System One Value...", ((OptionDemo)actual).OneProperty);
        }
        /*
        //数据库中已经有相应的记录时测试是否覆盖
        [TestMethod()]
        public void SaveToSystemTest2()
        {
            TheOptionData ts = new TheOptionData();
            SettingManager<TheOptionData>.SaveToSystem(new TheOptionData(4, "hello"));
            SettingManager<TheOptionData>.SaveToSystem(ts);
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromSystem();
            Assert.AreEqual(ts.OneProperty, testSetting.OneProperty);
        }

        [TestMethod()]
        //在数据库有系统值的情况下
        public void GetFromSystemTest()
        {
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromSystem();
            Assert.AreEqual(new TheOptionData().OneProperty, testSetting.OneProperty);
        }


        [TestMethod()]
        //在数据库没有系统值的情况下测试
        public void GetFromSystemTest2()
        {

            SettingManager<TheOptionData>.ClearForSystem();
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromSystem();
            Assert.AreEqual(null, testSetting);
        }

        //当数据库有Setting有对应的记录时
        [TestMethod()]
        public void GetFromUserTest()
        {
            TheOptionData ts = new TheOptionData();
            SettingManager<TheOptionData>.SaveToUser(ts);
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromUser();
            Assert.AreEqual(ts.OneProperty, testSetting.OneProperty);
        }

        //当数据库没有User对应的记录时
        [TestMethod()]
        public void GetFromUserTest2()
        {
            SettingManager<TheOptionData>.ClearForUser();
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromUser();
            Assert.IsNull(testSetting);
        }


        //在数据库中没有setting的记录时
        [TestMethod()]
        public void SaveToUserTest()
        {
            ////清楚setting的TesSetting的值
            //SettingEntity.DeleteBySettingContext_TypeName(AppContext.Current.SettingContextName, "TestSetting");  
            //TheOptionData ts = new TheOptionData();
            //Setting<TheOptionData>.SaveToUser(ts);
            //TheOptionData testSetting = Setting<TheOptionData>.GetFromUser();
            //Assert.AreEqual(ts.OneProperty, testSetting.OneProperty);
        }

        //当数据库中已经有相应的user记录时
        [TestMethod()]
        public void SaveToUserTest2()
        {
            ////清楚setting的TesSetting的值
            //SettingEntity.DeleteBySettingContext_TypeName(AppContext.Current.SettingContextName, "TestSetting");
            //TheOptionData ts = new TheOptionData();
            //Setting<TheOptionData>.SaveToUser(new TheOptionData(3,"hello"));
            //Setting<TheOptionData>.SaveToUser(ts);
            //TheOptionData testSetting = Setting<TheOptionData>.GetFromUser();
            //Assert.AreEqual(ts.OneProperty, testSetting.OneProperty);
        }

        [TestMethod()]
        public void ClearForSystemTest()
        {
            SettingManager<TheOptionData>.ClearForSystem();
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromSystem();
            Assert.AreEqual(null, testSetting);
            
        }

        //数据库中没有Setting的记录时
        [TestMethod()]
        public void ClearForSystemTest2()
        {
            SettingManager<TheOptionData>.ClearForSystem();
            SettingManager<TheOptionData>.ClearForSystem();
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromSystem();
            Assert.AreEqual(null, testSetting);

        }

        //数据中有相应的User记录时
        [TestMethod()]
        public void ClearForUserTest()
        {
            SettingManager<TheOptionData>.ClearForUser();
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromUser();
            Assert.AreEqual(null, testSetting);

        }

        //数据库中没有User的记录时
        [TestMethod()]
        public void ClearForUserTest2()
        {
            SettingManager<TheOptionData>.ClearForUser();
            SettingManager<TheOptionData>.ClearForUser();
            TheOptionData testSetting = SettingManager<TheOptionData>.GetFromUser();
            Assert.IsNull(testSetting);

        }

        [TestMethod()]
        //在有用户数据的时候得到用户数据
        public void GetOptionDataTest()
        {
            TheOptionData ts = new TheOptionData();
            SettingManager<TheOptionData>.SaveToUser(ts);
            TheOptionData test = SettingManager<TheOptionData>.GetOptionData();
            Assert.AreEqual(ts.OneProperty, test.OneProperty);

        }


        [TestMethod()]
        //在没有用户数据的时候得到系统数据
        public void GetOptionDataTest2()
        {
            TheOptionData ts = new TheOptionData(3,"optionDataTest");
            SettingManager<TheOptionData>.ClearForUser();
            SettingManager<TheOptionData>.SaveToSystem(ts);
            TheOptionData test = SettingManager<TheOptionData>.GetOptionData();
            Assert.AreEqual(ts.OneProperty, test.OneProperty);

        }

        [TestMethod()]
        //在没有系统数据的时候读取默认数据
        public void GetOptionDataTest3()
        {
            TheOptionData ts = new TheOptionData();
            ts.Reset();
            SettingManager<TheOptionData>.ClearForUser();
            SettingManager<TheOptionData>.ClearForSystem();
            TheOptionData test = SettingManager<TheOptionData>.GetOptionData();
            Assert.AreEqual(ts.OneProperty, test.OneProperty);

        }
        */
    }
}
